package com.cloudkinto.controller.cost;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.cost.vo.CostItemGetReq;
import com.cloudkinto.service.detective.vo.pick.BatchReq;
import com.cloudkinto.vo.cost.CostPageVo;
import com.cloudkinto.service.cost.CostService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author hua
 * @since 2024-11-29
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "业务费用oms模块")
@RequestMapping("/api/admin/oms/Cost")
public class CostOmsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(CostOmsController.class);

    private final CostService service;


    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功",service.detail(id, getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit("omp", getCurrentUserId(), getCompanyId(), getTenantId());
    }


    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "whIds", value = "仓库id 多个,隔开"),
            @ApiImplicitParam(name = "costStatuses", value = "计费状态 1未计费 2已计费  3重新计费中 4计费中"),
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 记费时间 cost_time 费用单据创建时间 create_time oms创建时间 oms_order_create_time"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "costNo", value = "费用单据号"),
            @ApiImplicitParam(name = "orderNo", value = "OMS单据号"),
            @ApiImplicitParam(name = "currencyCodes", value = "币种"),
            @ApiImplicitParam(name = "orderTypes", value = "单据类型 多个逗号给开"),
            @ApiImplicitParam(name = "moduleTypes", value = "模块类型 多个逗号给开"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = CostPageVo.class))
    public PageResult listPage(HttpServletRequest request){

        Map<String, Object> map = this.getMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }


    public  Map<String, Object> getMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", getTenantId());
        map.put("companyId", getCompanyId());
        map.put("userId", getCurrentUserId());
        map.put("source", "oms");
        String status = request.getParameter("status");
        if (StringUtils.isNotBlank(status)) {
            map.put("status", status);
        }
        String whIds = request.getParameter("whIds");
        if (StringUtils.isNotBlank(whIds)) {
            map.put("whIds", Arrays.asList(whIds.split(",")));
        }
        String costStatuses = request.getParameter("costStatuses");
        if (StringUtils.isNotBlank(costStatuses)) {
            map.put("costStatuses", Arrays.asList(costStatuses.split(",")));
        }
        String dateColumn = request.getParameter("dateColumn");
        if (StringUtils.isNotBlank(dateColumn)) {
            map.put("dateColumn", dateColumn);
        }
        String beginTime = request.getParameter("beginTime");
        if (StringUtils.isNotBlank(beginTime)) {
            map.put("beginTime", beginTime);
        }
        String endTime = request.getParameter("endTime");
        if (StringUtils.isNotBlank(endTime)) {
            map.put("endTime", endTime);
        }
        String costNo = request.getParameter("costNo");
        if (StringUtils.isNotBlank(costNo)) {
            map.put("costNo", costNo);
        }
        String orderNo = request.getParameter("orderNo");
        if (StringUtils.isNotBlank(orderNo)) {
            map.put("orderNo", orderNo);
        }
        String moduleType = request.getParameter("moduleType");
        if (StringUtils.isNotBlank(moduleType)) {
            map.put("moduleType", moduleType);
        }
        String moduleTypes = request.getParameter("moduleTypes");
        if (StringUtils.isNotBlank(moduleTypes)) {
            map.put("moduleTypes", Arrays.asList(moduleTypes.split(",")));
        }
        String orderTypes = request.getParameter("orderTypes");
        if (StringUtils.isNotBlank(orderTypes)) {
            map.put("orderTypes", Arrays.asList(orderTypes.split(",")));
        }
        String currencyCodes = request.getParameter("currencyCodes");
        if (StringUtils.isNotBlank(currencyCodes)) {
            map.put("currencyCodes", Arrays.asList(currencyCodes.split(",")));
        }
        return map;

    }


    @UserSessionRequired
    @GetMapping("/exportCost")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id 多个,隔开"),
            @ApiImplicitParam(name = "whIds", value = "仓库id 多个,隔开"),
            @ApiImplicitParam(name = "costStatuses", value = "计费状态 1未计费 2已计费  3重新计费中 4计费中"),
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 记费时间 cost_time 费用单据创建时间 create_time oms创建时间 oms_order_create_time"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "costNo", value = "费用单据号"),
            @ApiImplicitParam(name = "orderNo", value = "OMS单据号"),
            @ApiImplicitParam(name = "currencyCodes", value = "币种"),
            @ApiImplicitParam(name = "orderTypes", value = "单据类型 多个逗号给开"),
            @ApiImplicitParam(name = "moduleTypes", value = "模块类型 多个逗号给开"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
    })
    public void exportCost(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> map = this.getMap(request);
        String ids = request.getParameter("ids");
        if (StringUtils.isNotBlank(ids)) {
            map.put("ids", Arrays.asList(ids.split(",")));
        }
        service.exportCost(map, getTenantId(), response);
    }

    @UserSessionRequired
    @PostMapping("/getCostItem")
    @ApiOperation(value = "获取费用详情", notes = "获取费用详情")
    public SingleResult getCostItem(@RequestBody @Validated CostItemGetReq req) {
        return SingleResult.success(service.getCostItem(req.getIds(), req.getOrderType(), getTenantId()));
    }
}

